package com.wooboo.server.ext;


import org.apache.log4j.PropertyConfigurator;

public class Logger {
	private final static String LOG4J_NAME = "logMain";
	private final static String LOG4J_PATH = "config/log4j.properties";
	private org.apache.log4j.Logger logger = null;
	private static Boolean init = Boolean.FALSE;
	
	
    
	public Logger() {
		initLogger();
//		if (!isDuappServer) {
	        logger = org.apache.log4j.Logger.getLogger(LOG4J_NAME);		    
//		}
	}

	public Logger(String logName) {
		initLogger();
//        if (!isDuappServer) {
		    logger = org.apache.log4j.Logger.getLogger(logName);
//        }
	}

	public Logger(Class<?> callerClass) {
		initLogger();
//        if (!isDuappServer) {
            logger = org.apache.log4j.Logger.getLogger(callerClass);
//        }
	}
	
	public final void logInfo(Object message) {
//        if (!isDuappServer) {
            logger.info(message);
//        }
	}
	public final void logError(String message){
		logger.error(message);
	}

	private static void initLogger(){
		if (!init.booleanValue()) {
			synchronized (init) {
				if (!init.booleanValue()) {
					//PropertyConfigurator.configure(LOG4J_PATH);
					
					
					PropertyConfigurator.configure(Thread.currentThread().getContextClassLoader().getResource(LOG4J_PATH));  //log4j.properties 
					init = Boolean.TRUE;
				}
			}
		}
	}
	
	
    public void open() {
        
    }
    
    public void close() {
        
    }
    public  void info(String template,Object ...params){
    	String content = composeLog(template, params);
    	logger.info(content);
    }
    public void trace(String template, Object... params) {
//        if (!isDuappServer) {
            String content = composeLog(template, params);
        	logger.info(content);
//        }
    }
    
    public void debug(String template, Object... params) {
//        if (!isDuappServer) {
            String content = composeLog(template, params);
            logger.debug(content);
//        }
    }
    
    public static Logger createInstance() {
    	initLogger();
        return new Logger();
    }
	
    public static Logger getLogger(String logName) {
		return new Logger(logName);
	}
	public static Logger getLogger(Class<?> callerClass) {
		return new Logger(callerClass);
	}

	private static String composeLog(String template, Object...params) {
        String content = template;
        if (params != null && params.length > 0) {
            try{
                content = String.format(template, params);          
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return content;
	}
	
    public static void main(String[] args) throws Exception{
        System.out.println(composeLog("hello, world"));
        System.out.println(composeLog("Hello, %s. Love you %d times."));
        System.out.println(composeLog("Hello, %s. Love you %d times.", "kitty", 100));
    }
}
